<?php
namespace app\cron\controller;
class caijiController extends \app\base\controller\BaseController
{

    public function dtk(){
      for($key='1';$key<=10;$key++){
		  self::dtkcaiji($key,1,10);
	  }

    	$this->display();

    }

    public function dtkcaiji($page,$t,$end){
    	 
	      
	      $now=$page;
	      if($end==$now){ 
	        exit('ok');
	     }
		 include CONFIG_PATH . 'apiset.php';
    	 $token= new \ZhiCms\ext\weixin;
         $host = 'https://openapi.dataoke.com/api/goods/get-goods-list';
         $appKey = $api['appKey'];//应用的key
        $appSecret = $api['appSecret'];//应用的Secret
        //默认必传参数
        $datas = [
    'appKey' => $appKey,
    'version' => '1.1.0',
      ];
     //加密的参数
	  $datas['pageId']=$page;
     $datas['sign'] = $this->makeSign($datas,$appSecret);
	 //拼接请求地址
     $url = $host .'?'. http_build_query($datas);
		 $ret=obj("api/Api")->object_array(json_decode($token->http($url)));
$a = obj('api/ApiData')->thisquery('BEGIN');
 for($key='0';$key<=count($ret['data']['list'])-1;$key++){
	 $where[]="`goodsId` ='{$ret['data']['list'][$key]['goodsId']}'";
	 $chongfu=obj("api/Apidata")->Data_Select("youhuiquan",$where);
	 if(!$chongfu){
	        $data['goodsId']=$ret['data']['list'][$key]['goodsId'];
         	$data['itemLink']=$ret['data']['list'][$key]['itemLink'];
			$data['title']=$ret['data']['list'][$key]['title'];
         	$data['dtitle']=$ret['data']['list'][$key]['dtitle'];
         	$data['desc']=$ret['data']['list'][$key]['desc'];
         	$data['cid']=$ret['data']['list'][$key]['cid'];
         	$data['mainPic']=$ret['data']['list'][$key]['mainPic'];
         	$data['marketingMainPic']=$ret['data']['list'][$key]['marketingMainPic'];
         	$data['originalPrice']=$ret['data']['list'][$key]['originalPrice'];
         	$data['actualPrice']=$ret['data']['list'][$key]['actualPrice'];
         	$data['discounts']=$ret['data']['list'][$key]['discounts'];
         	$data['commissionType']=$ret['data']['list'][$key]['commissionType'];
         	$data['commissionRate']=$ret['data']['list'][$key]['commissionRate'];
         	$data['couponLink']=$ret['data']['list'][$key]['couponLink'];
			$data['couponTotalNum']=$ret['data']['list'][$key]['couponTotalNum'];
			$data['couponReceiveNum']=$ret['data']['list'][$key]['couponReceiveNum'];
			$data['couponEndTime']=$ret['data']['list'][$key]['couponEndTime'];
			$data['couponStartTime']=$ret['data']['list'][$key]['couponStartTime'];
         	$data['couponPrice']=$ret['data']['list'][$key]['couponPrice'];
			$data['couponConditions']=$ret['data']['list'][$key]['couponConditions'];
			$data['monthSales']=$ret['data']['list'][$key]['monthSales'];
         	$data['shopType']=$ret['data']['list'][$key]['shopType'];
         	$data['shopName']=$ret['data']['list'][$key]['shopName'];
			$data['hotPush']=$ret['data']['list'][$key]['hotPush'];
			$data['laiyuan']=2;
            obj("api/Apidata")->Inserts("youhuiquan",$data);			
	 }else{
		 $sql = "UPDATE `{pre}youhuiquan`  SET  originalPrice='{$ret['data']['list'][$key]['originalPrice']}',actualPrice='{$ret['data']['list'][$key]['actualPrice']}' ,discounts='{$ret['data']['list'][$key]['discounts']}',couponPrice='{$ret['data']['list'][$key]['couponPrice']}',commissionType='{$ret['data']['list'][$key]['commissionType']}',commissionRate='{$ret['data']['list'][$key]['commissionRate']}',couponReceiveNum='{$ret['data']['list'][$key]['couponReceiveNum']}',monthSales='{$ret['data']['list'][$key]['monthSales']}',hotPush='{$ret['data']['list'][$key]['hotPush']}'  WHERE  `goodsId` =  '{$ret['data']['list'][$key]['goodsId']}'";
	   obj('api/ApiData')->thisquery($sql); 
	 }
	 
if($key%100==0){
    //每1w条提交一次
    $d = obj('api/ApiData')->thisquery('COMMIT');
    $e = obj('api/ApiData')->thisquery('BEGIN');
}		
 echo 'ok';
 }
$f = obj('api/ApiData')->thisquery('COMMIT');

    }
	



	
	public function makeSign($data, $appSecret)
{
    ksort($data);
    $str = '';
    foreach ($data as $k => $v) {

        $str .= '&' . $k . '=' . $v;
    }
    $str = trim($str, '&');
    $sign = strtoupper(md5($str . '&key=' . $appSecret));
    return $sign;
}
}